<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: footprint.php Sat Jul 18 13:14:14 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") ) exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsFootprint extends XoopsObject 
{
	public function __construct() {
		$this->initVar("footprint_id", XOBJ_DTYPE_INT, null, false); // 脚印ID
		$this->initVar("group_id", XOBJ_DTYPE_INT,0,true); // 所属群组ID
		$this->initVar("uid", XOBJ_DTYPE_INT,0,true); // 访问者ID
		$this->initVar("footprint_display", XOBJ_DTYPE_INT,0,false); // 访问者ID
		$this->initVar("footprint_create_time", XOBJ_DTYPE_INT,0,false); // 来访的时间
		$this->initVar("footprint_update_time", XOBJ_DTYPE_INT,0,false); // 上次来访的时间
	}
	
}

class groupsFootprintHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_footprint","groupsFootprint","footprint_id");
    }
    
    /**
     * create and modify footprint
     *
     * @param array $params
     * @return int $id (footprint)
     */
    public function setFootprint( array $params, $force=true ) {
        extract($params);
        if ( empty($uid) || empty($group_id) ) return false;
        $row = array(
            'criteria'=>array('group_id'=>$group_id,'uid'=>$uid),
            'limit' => 1
        );
        if ( $objs = $this->getFootprints($row) ) {
            $_objs = array_values($objs) ;
            $obj = $_objs[0];
            $time = time();
            if ( $time - $obj->getVar("footprint_update_time") <= 600 ) {
                return (int)$obj->getVar("footprint_id");
            }
        }
        if ( !is_object($obj) ) {
            $obj = $this->get();
            $obj->setVar("footprint_create_time",$time);
        }
        $obj->setVar("footprint_update_time",$time);
        $obj->setVar("uid",(int)$uid);
        $obj->setVar("group_id",(int)$group_id);
    	if ( $id = $this->insert($obj,$force) ) {
    	    return $id;
    	}
    	return false;
    }
    
    /**
     * update footprint table fields
     *
     * @param array $params
     * @return int $id
     */
    public function updateFields( array $params ) {
        extract($params);
        $myts = MyTextSanitizer::getInstance();
        if ( empty($uid) || empty($group_id) ) return false;
        if ( $objs = $this->getFootprints( array('criteria'=>array('group_id'=>$group_id,'uid'=>$uid)) ) ) {
            $_objs = array_values($objs) ;
            $obj = $_objs[0];
        }
        if ( !is_object($obj) || $obj->isNew() ) return false;
        foreach ( $params as $key => $val ) {
	       if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
    	   $obj->setVar($key, $val);
    	}
    	if ( $id = $this->insert($obj) ) {
    	    return $id;
    	}
    	return false;
    }
    
    public function getGroupIds( array $uids ) {
        $params = array(
            'criteria' => array(
                'uid' => $uids
            ),
            'fields' => array('group_id'),
        );
        $objs = $this->getFootprints($params);
        $ret = array();
        if ( $objs ) {
            foreach ( $objs as $k=>$obj ) {
                $ret[] = $obj->getVar("group_id",'n');
            }
        }
        $ret = array_unique($ret);
        return $ret;
    }
    
    /**
     * select footprint list 
     *
     * @param array $params
     * @param bool $asObj
     * @return array $ret array/object array
     */
    public function getFootprints( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
    /**
     * for footprint page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $start, "start",$extra);
        	$pagenav = $nav->renderNav(4);
        }
        return $pagenav;
    }
}